<template>
  <div class="body">
    <div class="container">
      <div class="phone">
        <div class="title">手机号:</div>
        <div class="input">
          <el-input
            placeholder="130****0000"
            suffix-icon="el-icon-phone"
            v-model="phone"
          >
          </el-input>
        </div>
      </div>
      <div class="verificationCode">
        <div class="title">密码:</div>
        <div class="input">
          <el-input
            placeholder=""
            suffix-icon="el-icon-s-comment"
            v-model="verificationCode"
          >
          </el-input>
        </div>
        <!-- <div class="button_verification">

        </div> -->
      </div>
      <div class="button">
        <el-button type="warning" style="margin: auto" @click="clear"
          >重置</el-button
        >
        <el-button type="success" style="margin: auto" @click="doLogin"
          >登录</el-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { userAxios } from "../common/request/request.js";
export default {
  data() {
    return {
      phone: "",
      verificationCode: "",
    };
  },
  methods: {
    clear: function () {
      this.phone = "";
      this.verificationCode = "";
    },
    sendVerificationCode() {
      if (this.phone != "") {
        userAxios({
          url: "/user/sendVerificationCode",
          params: {
            phone: this.phone,
          },
          method: "get",
        }).then((res) => {
          if (res.code == 202) {
            this.$message({
              type: "warning",
              message: res.data + "秒后可重新发送",
            });
          } else {
            this.$message({
              type: "success",
              message: "验证码已成功",
            });
          }
        });
      } else {
        this.$message({
          type: "error",
          message: "手机号不可为空",
        });
      }
    },
    doLogin() {
      if (this.phone == "" || this.verificationCode == "") {
        this.$message({
          type: "error",
          message: "不可为空",
        });
      } else {
        userAxios({
          url: "/user/doLogin",
          method: "get",
          params: {
            phone: this.phone,
            verificationCode: this.verificationCode,
          },
        }).then((res) => {
          console.log(res);
          if (res.code == 205) {
            this.$message({
              type: "error",
              message: "验证码不正确",
            });
          } else if (res.code == 204) {
            this.$message({
              type: "success",
              message: "登陆成功",
            });
            this.$store.commit('setToken',res.data)
            this.$router.replace('/')
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.body {
  height: 100%;
  display: flex;
  justify-items: center;
  align-items: center;
  background-image: url("../../static/images/background.jpeg");
}

.container {
  width: 350px;
  height: 180px;
  border-radius: 10px;
  margin: auto;
  background-color: #93BCE7;
  border: 3px solid #dcdfe6;
  display: flex;
  flex-direction: column;
  padding: 30px;
}

.phone {
  flex: 1;
  display: flex;
  align-items: center;
  justify-items: center;
}

.title {
  flex: 1;
  text-align: left;
}

.input {
  flex: 3;
  display: flex;
}

.verificationCode {
  flex: 1;
  display: flex;
  align-items: center;
  justify-items: center;
}

.button {
  flex: 1;
  display: flex;
  align-items: center;
  justify-items: center;
}
</style>